#!/bin/bash
# This is the script for running SPECjvm
###SHELLPACK preamble specjvm-bench 0
###SHELLPACK monitor_hooks

STARTING_HUGEPAGES=`cat /proc/sys/vm/nr_hugepages`
COARSE_JOBLIST="compiler compress crypto derby mpegaudio scimark.large scimark.small serial startup sunflow xml"
FINE_JOBLIST="compiler.compiler compiler.sunflow compress crypto.aes crypto.rsa crypto.signverify derby mpegaudio scimark.fft.large scimark.lu.large scimark.sor.large scimark.sparse.large scimark.monte_carlo scimark.fft.small scimark.lu.small scimark.sor.small scimark.sparse.small serial sunflow xml.transform xml.validation"
JOBLIST=$COARSE_JOBLIST

export PATH=$SPECJVM_JRE:$PATH

# Defaults for a reportable run
if [ "$ITERATION_TIME" = "" ]; then
	ITERATION_TIME=$SPECJVM_DEFAULT_ITERATION_TIME
fi
if [ "$WARMUP_TIME" = "" ]; then
	WARMUP_TIME=$SPECJVM_DEFAULT_WARMUP_TIME
fi
if [ "$ITERATIONS" = "" ]; then
	ITERATIONS=$SPECJVM_DEFAULT_ITERATIONS
fi

ITERATION_TIME="-it $ITERATION_TIME"
WARMUP_TIME="-wt $WARMUP_TIME"
ITERATIONS=1

# Install support files ######################################################
if [ ! \( -e $SHELLPACK_SOURCES/specjvm \) ]; then
  	$SHELLPACK_INCLUDE/shellpack-install-specjvm
	check_status "$P: Installing specjvm"
fi
###SHELLPACK init_complete

# Ensure everything is where it is expected to be
pushd $SHELLPACK_SOURCES/specjvm > /dev/null || die Failed to cd to specjvm
[ ! -e SPECjvm2008.jar ] && die No specjvm jar available

# Clean out old results
[ -e results ] && rm -rf results

# Run the actual test
for PAGES in $SPECJVM_PAGESIZES; do

	USE_HUGEPAGE=
	case $PAGES in
		default)
			echo Using default pagesize configuration
			;;
		base)
			hugeadm --pool-pages-min DEFAULT:0
			hugeadm --pool-pages-max DEFAULT:0
			USE_HUGEPAGE=
			disable_transhuge
			;;
		huge)
			hugeadm --hard --pool-pages-min DEFAULT:4096M
			USE_HUGEPAGE=$SPECJVM_HUGESWITCH
			disable_transhuge
			;;
		transhuge)
			hugeadm --pool-pages-min DEFAULT:0
			hugeadm --pool-pages-max DEFAULT:0
			USE_HUGEPAGE=
			if [ "$TRANSHUGE_AVAILABLE" = "yes" ]; then
				enable_transhuge
			else
				echo THP support unavailable for transhuge
				continue
			fi
			;;

	esac

	for JOB in $JOBLIST; do
		monitor_pre_hook $LOGDIR_RESULTS $JOB
		java $USE_HUGEPAGE $SPECJVM_MAXHEAP -jar SPECjvm2008.jar $WARMUP_TIME $ITERATION_TIME --parseJvmArgs -i $ITERATIONS --peak $JOB -ikv
		monitor_post_hook $LOGDIR_RESULTS $JOB
	done

	mv results $LOGDIR_RESULTS/$PAGES
	for i in `seq 1 20`; do
		JOBID=`printf "%03d" $i`
		if [ -e gzip $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/SPECjvm2008.$JOBID.raw ]; then
			gzip $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/SPECjvm2008.$JOBID.raw
			gzip $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/SPECjvm2008.$JOBID.html
			tar -czf $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/images.tar.gz $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/images &&
				rm -rf $LOGDIR_RESULTS/$PAGES/SPECjvm2008.$JOBID/images
		fi
	done
done

exit $SHELLPACK_SUCCESS
